package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * aop监控增删改查运行时长
 */
@Component
@Aspect
public class RuntimeMonitorAdvice {
    //切入点 监控业务层接口
    @Pointcut("execution(* com.itheima.service.*Service.find*(..))")
    public void pt() {
    }

    //环绕通知
    @Around("pt()")
    public Object runtimeAround(ProceedingJoinPoint pjp) throws Throwable {
        //获取执行签名信息
        Signature signature = pjp.getSignature();
        //通过签名获取执行类型(接口名)
        String className = signature.getDeclaringTypeName();
        //通过签名获取执行操作名称(方法名)
        String methodName = signature.getName();

        //执行时长
        long sum = 0L;

        for (int i = 0; i <1000; i++) {
            //获取当前系统时间
            long stm = System.currentTimeMillis();
            //原始操作调用
            pjp.proceed(pjp.getArgs());
            //获取结束时间
            long edm = System.currentTimeMillis();
            //计算运行时间
            sum = edm - stm;
        }
        //输出运行时长
        System.out.println(className + ":" + methodName + "次run" + sum + "ms");
        //返回一个null结束程序
        return null;
    }
}
